//
//  LcTableViewController.m
//  Our Big Demo
//
//  Created by lanou3g on 16/3/22.
//  Copyright © 2016年 WJP. All rights reserved.
//

#import "LcTableViewController.h"
#import "TableHelder.h"
@interface LcTableViewController ()
@property (nonatomic, weak) SDRefreshFooterView *refreshFooter;
@property (nonatomic, weak) SDRefreshHeaderView *refreshHeader;
@property (nonatomic, strong) LcPrangePlay *coverFlowView2;
@property (nonatomic, strong) LcTableViewCell *cell;
@property (nonatomic, strong) NSTimer *time;
@property (nonatomic, assign) NSInteger totalRowCount;
@property (nonatomic, strong) NSMutableArray *dataArrray;
@property (nonatomic, strong) NSMutableArray *ScrollArray;
@property (weak, nonatomic) IBOutlet UISegmentedControl *chooseSegControl;
@property (nonatomic, strong) UIView *playView;
@property(nonatomic,strong)NSString *urlStr2;

@end

@implementation LcTableViewController

//刷新
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        
        self.tableView.rowHeight = 60.0f;
        self.tableView.separatorColor = [UIColor whiteColor];
        // 模拟数据
        _totalRowCount = 5;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.urlStr2 = @"&tab=latest";
    [self navigationControllerBackColor];
    [self JsonDataWithImageAndLable];
    [self setupHeader];
    [self setupFooter];
    [self.coverFlowView2 animationDuration];
    
    [self.cell.isMyStar setStars:4 callbackBlock:^(NSNumber *newRating) {
        NSLog(@"didChangeRating: %@",newRating);
    }];
    
    self.chooseSegControl.selectedSegmentIndex = 0;
    [self.chooseSegControl addTarget:self action:@selector(clicAction:) forControlEvents:UIControlEventValueChanged];

}
- (void)clicAction:(UISegmentedControl *)seg{
    if (seg.selectedSegmentIndex == 0) {
        NSLog(@"==0");
        self.urlStr2 = @"&tab=latest";
        [self JsonDataWithImageAndLable];
    }else{
        NSLog(@"==1");
        self.urlStr2 = @"&tab=hot";
        [self JsonDataWithImageAndLable];
    }
}
- (void) JsonDataWithImageAndLable {
    self.dataArrray = [NSMutableArray new];
    self.ScrollArray = [NSMutableArray new];
    

    
    for (int i = 1; i < 6 ; i++ ) {
        
        NSString *string = @"http://app.vmoiver.com/apiv3/post/getPostByTab?p=";
        NSString *string1 = [NSString stringWithFormat:@"%d",i];
        NSString *string3 = [string stringByAppendingString:string1];
        NSString *string4 = [string3 stringByAppendingString:self.urlStr2];
    [[GiveMeHleper defaultCDHttpHelper] get:string4 params:nil success:^(id responseObj) {
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:&error];
        
        NSArray *array = dic[@"data"];
        for (NSDictionary *dic in array) {
            UseModle *modle = [UseModle new];
            [modle setValuesForKeysWithDictionary:dic];
            [self.dataArrray addObject:modle];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    }
    
    [[GiveMeHleper defaultCDHttpHelper] get:@"http://app.vmoiver.com/apiv3/index/getBanner/"params:nil success:^(id responseObj) {
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:&error];
        
        NSArray *array = dic[@"data"];
        for (NSDictionary *dic in array) {
            UserMessage *message = [UserMessage new];
            [message setValuesForKeysWithDictionary:dic];
            [self.ScrollArray addObject:message];
            
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];

    
}

- (void) navigationControllerBackColor {
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"BackGround"] forBarMetrics:0];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.totalRowCount;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LcTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

   
    UseModle *modle = self.dataArrray[indexPath.row];
    [cell.LcImage sd_setImageWithURL:[NSURL URLWithString:modle.image]];
    cell.LcTime.text = modle.duration;
    cell.LcShare.text = modle.like_num;
    cell.LcNumber.text = modle.rating;
    cell.LcTitle.text = modle.title;
    
    return cell;
}

- (void)setupHeader
{
    SDRefreshHeaderView *refreshHeader = [SDRefreshHeaderView refreshView];
    
    [refreshHeader addToScrollView:self.tableView];
    
    __weak SDRefreshHeaderView *weakRefreshHeader = refreshHeader;
    __weak typeof(self) weakSelf = self;
    refreshHeader.beginRefreshingOperation = ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.totalRowCount += 5;
            [weakSelf.tableView reloadData];
            [weakRefreshHeader endRefreshing];
        });
    };
    
    // 进入页面自动加载一次数据
    [refreshHeader autoRefreshWhenViewDidAppear];
    
}

- (void)setupFooter
{
    SDRefreshFooterView *refreshFooter = [SDRefreshFooterView refreshView];
    [refreshFooter addToScrollView:self.tableView];
    [refreshFooter addTarget:self refreshAction:@selector(footerRefresh)];
    _refreshFooter = refreshFooter;
}


- (void)footerRefresh
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.totalRowCount += 5;
        [self.tableView reloadData];
        [self.refreshFooter endRefreshing];
    });
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 150;
    
}
//去掉 UItableview headerview 黏性(sticky)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 150; //sectionHeaderHeight
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section{
    
    self.playView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.tableView.frame.size.width, 150)];
    self.coverFlowView2 = [[LcPrangePlay alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    _coverFlowView2.backgroundColor = [UIColor clearColor];
    _coverFlowView2.layer.cornerRadius = 5.0;
    _coverFlowView2.layer.masksToBounds = YES;
    _coverFlowView2.pageItemWidth = self.view.frame.size.width / 3 * 2;
    _coverFlowView2.pageItemCoverWidth = 10.0;
    _coverFlowView2.pageItemHeight = 150.0;
    _coverFlowView2.pageItemCornerRadius = 8.0;
    [_coverFlowView2 setPageItemsWithImageNames:@[@"http://cs.vmoiver.com/Uploads/Banner/2016/03/22/56f12a8f6b400.jpg", @"http://cs.vmoiver.com/Uploads/Banner/2016/03/21/56efce53ef5b1.jpg", @"http://cs.vmoiver.com/Uploads/Banner/2016/03/21/56efcddf558d2.jpg", @"http://cs.vmoiver.com/Uploads/Banner/2016/03/17/56ea8933b8c6b.jpg", @"http://cs.vmoiver.com/Uploads/Banner/2016/03/18/56ec078a23f1d.jpg", @"http://cs.vmoiver.com/Uploads/Banner/2016/03/17/56ea8951bbbed.jpg"]];
    [self.playView addSubview:_coverFlowView2];
   
    return self.playView;
}

- (void)coverFlowView:(LcPrangePlay *)coverFlowView didScrollPageItemToIndex:(NSInteger)index {
    
    
}

- (void)coverFlowView:(LcPrangePlay *)coverFlowView didSelectPageItemAtIndex:(NSInteger)index {
}




- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    LCViewController *LcView = [[LCViewController alloc] init];
    UseModle *Modle = self.dataArrray[indexPath.row];
    LcView.getString = Modle.postid;
    [self.navigationController pushViewController:LcView animated:YES];
    
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
